home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / macros / plain / contrib / ruled.tex < prev    next >
Encoding:
Text File  |  1992-08-26  |  16.6 KB  |  375 lines

  1. % file: ruled.tex            TeXsis                  version 2.14
  2. % :: $Header: /usr2/myers/texsis/RCS/ruled.tex,v 1.5 90/06/21 13:47:44 myers Stab $
  3. %======================================================================*
  4. %  RULED TABLES.                                E. Myers and F.E. Paige
  5. %
  6. %       The following macros will typeset tables with vertical and
  7. % horizontal rules. The syntax is similar to Ray Cowan's TABLES.TEX,
  8. % but the macros have been completely rewritten. Vertical and horizontal 
  9. % rules are drawn by macros using vertical spacing controlled by struts.
  10. % Usage:
  11. %       \ruledtable
  12. %       <item> <tab> ... <item> <cr>
  13. %       ...
  14. %       <item> <tab> ... <item> \endruledtable
  15. % where <item> is an element of the table (every row must have exactly 
  16. % the same number of items, although some may be blank), <tab> is any
  17. % one of
  18. %     &                 for no vertical rule
  19. %     |  [or \vb]       for vertical rule
  20. %     \| [or \Vb]       for thick vertical rule
  21. %     \dbl              for double vertical rule
  22. % and <cr> is any one of
  23. %     \nr               for no horizontal rule
  24. %     \cr               for horizontal rule
  25. %     \CR [or \crthick] for thick horizontal rule
  26. % The last <cr> must be omitted in favour of the \endruledtable
  27. %
  28. %  For horizontal rules across only some columns use
  29. %       ... \nr
  30. %       \crule | \cskip & \Crule ... \crpart
  31. % where \crpart is a special \cr for the partial rule line and
  32. %       \cskip          no rule for column
  33. %       \crule          horizontal rule for column
  34. %       \Crule          thick horizontal rule for column
  35. % The number of columns must be identical to the other rows.
  36. %
  37. % To change the appearance of the table, modify \TableItem.
  38. %  Several modifications are built in:
  39. %       \LeftJustifyTables      left justify each column
  40. %       \RightJustifyTables     right justify each item
  41. %       \NoJustifyTables        center each item (default)
  42. %       \TightTables            use \ horizontal spacing
  43. %       \LooseTables            use \quad horizontal spacing (default)
  44. %
  45. % Use \bigitem to expand the vertical strut for high or deep items. 
  46. %
  47. %       These macros have been reorganized and substantially revised from
  48. % version 2.13. The following functional changes have been made:
  49. %       (1) The @ signs have been removed from the names of the macros
  50. % used to save the Plain \cr, \span, and & so that they can be used to
  51. % modify the preamble. See above.
  52. %       (2) Macros for partial horizontal rules have been added.
  53. %======================================================================*
  54. % Counters and such:     
  55. \catcode`@=11                                   % @ is a letter here
  56. \catcode`\|=12                                  % make sure | is not active
  57. \catcode`\&=4                                   % and that & is alignment tab
  58.  
  59. \newcount\ncols         \ncols=\z@              % number of columns in table
  60. \newcount\nrows         \nrows=\z@              % number of rows in table
  61. \newcount\curcol        \curcol=\z@             % current column counter
  62. \let\currow=\nrows                              % current row counter
  63.      
  64. \newdimen\thinsize      \thinsize=0.6pt         % thin rule width
  65. \newdimen\thicksize     \thicksize=1.5pt        % thick rule width
  66.  
  67. \newif\iftableinfo      \tableinfotrue          % report rows and columns? Yes
  68. \newif\ifcentertables   \centertablestrue       % center tables? Yes
  69. \def\centeredtables{\centertablestrue}%
  70. \def\noncenteredtables{\centertablesfalse}%
  71. \def\nocenteredtables{\centertablesfalse}%      % synonym !
  72.      
  73. \let\plaincr=\cr                        % save real \cr
  74. \let\plainspan=\span                    % save real \span
  75. \let\plaintab=&                         % save real alignment tab &
  76. \def\ampersand{\char`\&}%               % to print `&' in text
  77. \let\lparen=(                           % save left paren     
  78. \let\NX=\noexpand                       % shorthand for \noexpand is \NX
  79.  
  80. %---------------------------------------*
  81. % Main macros:
  82. %       \ruledtable <table stuff> \endruledtable turns on the ruled
  83. % table definitions and makes the table with \halign. It handles all
  84. % the control stuff; the real work is done by \@RuledTable.
  85.      
  86. \def\ruledtable{\relax                          % make ruled table
  87.     \@BeginRuledTable                           % initialize table
  88.     \@RuledTable}%                              % now process table body
  89.  
  90. %  \@BeginRuledTable does all the work of setting things up before
  91. % we read in the body of the table
  92.  
  93. \def\@BeginRuledTable{%                         % initialize table
  94.    \ncols=0\nrows=0                             % reset row and column count
  95.    \begingroup                                  % keep the following local
  96.     \offinterlineskip                           % so vrules touch
  97.     \def~{\phantom{0}}%                         % ~ is phantom digit
  98.     \def\span{\plainspan\omit\relax\colcount\plainspan}%  \span USER columns
  99.     \let\cr=\crrule                             % \cr gives a \tablerule
  100.     \let\CR=\crthick                            % \CR gives a \thickrule
  101.     \let\nr=\crnorule                           % \nr give no rule
  102.     \let\|=\Vb                                  % thick vrule between columns
  103. % support old \tablestrut in place of \tstrut if it exists
  104. %
  105.     \ifx\tablestrut\undefined\relax             % if not defined, no problem
  106.     \else\let\tstrut=\tablestrut\fi             % use \tablestrut
  107. %
  108.     \catcode`\|=13 \catcode`\&=13\relax         % make | and & active
  109.     \TableActive                                % | and & get active \def`s
  110.     \curcol=1                                   % reset column count
  111. %
  112. % define \Halign to do an \halign with or without a width
  113. %
  114.     \ifdim\tablewidth>-\maxdimen\relax          %
  115.       \edef\@Halign{\NX\halign to \NX\tablewidth\NX\bgroup\TablePreamble}% 
  116.       \tabskip=0pt plus 1fil                    % let things stretch
  117.     \else                                       %
  118.       \edef\@Halign{\NX\halign\NX\bgroup\TablePreamble}% 
  119.       \tabskip=0pt                              % no stretch between columns
  120.     \fi                                         %
  121. %
  122. % center text if needed
  123. %
  124.     \ifcentertables                             % should table be centered?
  125.        \ifhmode\vskip 0pt\fi                    % yes: force vertical mode
  126.        \line\bgroup\hss                         % center across page
  127.     \else\hbox\bgroup                           % else: just put in \hbox
  128.     \fi}%                                       % end of \@BeginRuledTable
  129.  
  130.  
  131. %       \@RuledTable builds the table with \@Halign and getting the
  132. % table body text as its argument.
  133.  
  134. \long\def\@RuledTable#1\endruledtable{%         % ruled table alignment
  135.    \vrule width\thicksize                       % thick rule on side
  136.      \vbox{\@Halign                             % then do \halign
  137.        \thickrule                               % thick rule on top
  138.        #1\relax                                 % body of table
  139.        \tstrut                                  % vertical strut for last line
  140.        \plaincr\thickrule                       % \cr, thick rule on bottom
  141.      \egroup}%                                  % end of \halign and \vbox
  142.    \vrule width\thicksize                       % thick rule on side, end \hbox
  143.    \ifcentertables\hss\fi\egroup                % finish table centering
  144.   \endgroup                                     % end group from \ruledtable
  145.   \global\tablewidth=-\maxdimen                 %   and reset width
  146.   \iftableinfo                                  % report rows and columns
  147.       \immediate\write16{[Nrows=\the\nrows, Ncols=\the\ncols]}%
  148.    \fi}%                                        % end of \@RuledTable
  149.      
  150. %---------------------------------------*
  151. % Preamble and item macros:
  152. %       This is the preamble for the \halign in \ruledtable. 
  153. % To change how each item is processed change \TableItem.
  154. % To make a more complicated table you can change the \TablePreamble,
  155. % but if yo do so use the following substitutions in a standard \halign 
  156. % preamble:
  157. %    for  &     use     \plaintab
  158. %    for  \cr   use     \plaincr
  159. %    for  #     use     ##
  160. %    for  ##    use     ####
  161. % and put \linecount in the first column so that line counting
  162. % works properly.
  163.  
  164. \def\TablePreamble{%                    % \ruledtable preamble
  165.    \linecount                           % count this line
  166.    \TableItem{####}%                    % the first item
  167.    \plaintab\plaintab                   % && means repeat this
  168.    \TableItem{####}%                    % the subsequent items
  169.    \plaincr}%                           % end of preamble
  170.  
  171. %       \TableItem contains glue or spacing around the item
  172.  
  173. \def\@TableItem#1{%                             % centers item in ruled table
  174.    \hfil\tablespace                             % left glue
  175.    #1\relax                                     % item
  176.    \tablespace\hfil                             % right glue
  177.     }%
  178.  
  179. \def\@tableright#1{%                    % right justifies item in ruled table
  180.    \hfil\tablespace\relax               % left glue
  181.    #1\relax                             % item
  182.    \tablespace\relax}%                  % right glue
  183.  
  184. \def\@tableleft#1{%                     % left justifies item in ruled table
  185.    \tablespace\relax                    % left glue
  186.    #1\relax                             % item
  187.    \tablespace\hfil}%                   % right glue
  188.  
  189. \let\TableItem=\@TableItem              % default is centered
  190.      
  191. \def\RightJustifyTables{\let\TableItem=\@tableright}%   % to right justify
  192. \def\LeftJustifyTables{\let\TableItem=\@tableleft}%     % to left justify
  193. \def\NoJustifyTables{\let\TableItem=\@TableItem}%       % to center
  194.  
  195. \def\LooseTables{\let\tablespace=\quad}%        % table spacing is \quad
  196. \def\TightTables{\let\tablespace=\space}%       % table spacing is space
  197. \LooseTables                                    % default is \quad
  198.  
  199. %---------------------------------------*
  200. % Table Height and Width:
  201. %
  202. %  Normally tables are set to their natural width.  If \tablewidth
  203. %  has been set then we set the table to that width instead,
  204. %  but only for the next table.  Then \tablewidth is turned off
  205.  
  206.  
  207. \newdimen\tablewidth    \tablewidth=-\maxdimen  % start ``turned off''
  208.  
  209. %      \setRuledStrut sets up the vertical strut \tstrut with
  210. % the appropriate dimensions to hold up one line of a ruled table. 
  211.  
  212.  
  213. \def\setRuledStrut{% sets interlines spacing for ruled tables
  214.    \dimen@=\baselineskip                        % \dimen@ = extra space
  215.    \advance\dimen@ by-\normalbaselineskip       % between lines
  216.    \ifdim\dimen@<.5ex \dimen@=.5ex\fi           % minimum space
  217.    \setbox0=\hbox{\lparen}%                     % get character size
  218.    \dimen1=\dimen@ \advance\dimen1 by \ht0      % space above line
  219.    \dimen2=\dimen@ \advance\dimen2 by \dp0      % space below line
  220.    \def\tstrut{\vrule height\dimen1 depth\dimen2 width\z@}%
  221.    }%
  222.  
  223. \def\tstrut{\vrule height 3.1ex depth 1.2ex width 0pt}%  default
  224.  
  225.  
  226. %      \tstrut does not produce the correct spacing if the entry in
  227. % the table is too high. The following constructs a strut higher than its
  228. % argument and then prints the argument. The minimum space value should
  229. % be the same as in \setRuledStrut. 
  230.  
  231.  
  232. \def\bigitem#1{%                                % larger table entry
  233.    \setbox0=\hbox{#1}%                          % put arg. in box and
  234.    \dimen1 =\ht0 \dimen2 =\dp0                  % get its size
  235.    \dimen@ =\baselines@ve                       % \dimen@ = extra space
  236.    \advance\dimen@ by-\normalbaselineskip       %   between lines
  237.    \ifdim\dimen@<.25ex \dimen@=.25ex\fi         % minimum space
  238.    \advance\dimen1 by \dimen@                   % space above line
  239.    \advance\dimen2 by \dimen@                   % space below line
  240.    \vrule height\dimen1 depth\dimen2 width\z@   % make strut to size
  241.    \copy0}%                                     % print argument
  242.  
  243.  
  244. %       \vctr{stuff} centers the stuff vertically, so that it can
  245. % appear between two ROWS.
  246.      
  247. \def\vctr#1{\hfil\vbox to 0pt{\vss\hbox{#1}\vss}\hfil}%
  248.  
  249. %---------------------------------------*
  250. % Vertical rules:
  251. %       \tab, \vb and \Vb are used in a table to separate columns with no
  252. % rule, a thin rule, or a thick rule, respectively.  \nextcolumn{<rule>} skips
  253. % to  the next column and puts the <rule> between columns.  Use this to build
  254. % your own separators.
  255.      
  256. \def\nextcolumn#1{%                             % move to next col.
  257.    \plaintab\omit#1\relax\colcount              % tab, insert #1, count
  258.    \plaintab}%                                  % tab to next user col.
  259.      
  260. \def\tab{%                                      % no rule between columns
  261.    \nextcolumn{\relax}}%                        % count column
  262.  
  263. \let\novb=\tab                                  % synonym for \tab
  264.  
  265. \def\vb{%                                       % thin rule between columns
  266.    \nextcolumn{\vrule width\thinsize}}%         % count and rule
  267.  
  268. \def\Vb{%                                       % THICK rule between columns
  269.    \nextcolumn{\vrule width\thicksize}}%        % count and thick rule
  270.  
  271. \def\dbl{%                                      % double rule between columns
  272.    \nextcolumn{\vrule width\thinsize            % count and rule
  273.    \hskip\thinsize\vrule width\thinsize}}%      % and skip and rule
  274.  
  275. %       \TableActive makes | the same as \vb and & the same as \tab so
  276. % these single characters can be used between columns. Here we have to
  277. % make & and | active so we get the active version of the characters,
  278. % hence the temporary change of \catcode.
  279.      
  280. {\catcode`\|=13 \let|0
  281.  \catcode`\&=13 \let&0
  282.  \gdef\TableActive{\let|=\vb \let&=\tab}%
  283. }% end \catcode`s
  284.  
  285. %---------------------------------------*
  286. % Horizontal rules:
  287. %       These replacements for \cr put a wide \vrule at the end of the
  288. % line and maybe put a rule under the line, then begin the next line
  289. % with the wide \vrule from the preamble.
  290.      
  291.  
  292. \def\crrule{\relax                      % \cr plus rule
  293.    \tstrut                              % strut for spacing
  294.    \plaincr\tablerule                   % \cr, regular rule below line
  295.   }%
  296.  
  297. \def\crthick{\relax                     % \cr plus thick rule
  298.    \tstrut                              % strut for vertical spacing
  299.    \plaincr\thickrule                   % \cr, rule, begin next line
  300.   }%                
  301.      
  302. \def\crnorule{\relax                    % \cr plus no rule
  303.    \tstrut                              % strut for spacing
  304.    \plaincr                             % \cr, norule, begin next line
  305.    }%
  306.    
  307. \def\crpart{\plaincr}%                  % for partial rules, no strut
  308.  
  309.  
  310. %       These rules go across the table.
  311.      
  312. \def\tablerule{\noalign{\hrule height\thinsize depth 0pt}}%
  313. \def\thickrule{\noalign{\hrule height\thicksize depth 0pt}}%
  314.  
  315.  
  316. %       Rules for individual columns. You must use \cskip in columns
  317. % with no rules to \omit the \TablePreamble.
  318.  
  319. \def\cskip{\omit\relax}%
  320. \def\crule{\omit\leaders\hrule height\thinsize depth0pt\hfill}%
  321. \def\Crule{\omit\leaders\hrule height\thicksize depth0pt\hfill}%
  322.      
  323. %---------------------------------------*
  324. % Counting macros:     
  325. %       These macros count rows and columns of the table. After the
  326. % first line has been processed \the\ncols is the total number of
  327. % columns in the table, which may be useful. During processing,
  328. % \the\curcol is the number of the current column, while \the\currow is
  329. % the number of the current row.
  330.      
  331.  
  332. \def\linecount{\relax\global\ncols=\curcol      % save column count in \ncols
  333.    \global\curcol=1                             % and reset counter
  334.    \global\advance\nrows by 1\relax}%           % and advance row counter
  335.      
  336. \def\colcount{\relax                            %
  337.    \global\advance\curcol by 1\relax}%          % advance column counter
  338.  
  339. %---------------------------------------*
  340. % TEXT TABLES.
  341. %  To put text in a table use \para{<text>}, with \parasize set
  342. %  to the desired width of the text.
  343.  
  344.  
  345. \newdimen\parasize      \parasize=4in           % paragraph size in tables
  346.  
  347. \long\def\para#1{%
  348.   \vtop{\hsize=\parasize                %       
  349.    \baselineskip=14pt \lineskip=1pt     %
  350.    \lineskiplimit=1pt                   %
  351.    \noindent #1\relax                   %
  352.    \vrule width 0pt depth 6pt}%         % hold depth of box
  353. }%
  354.  
  355. %---------------------------------------*
  356. % TABLES.TEX
  357. %   For compatability with Cowan's TABLES.TEX we also allow the
  358. % syntax \begintable ... \endtable, which do the same thing.
  359. %
  360.  
  361. \def\begintable{\relax                          % make ruled table
  362.     \@BeginRuledTable                           % initialize table
  363.     \@begintable}%                              % now process table body
  364.  
  365. \long\def\@begintable#1\endtable{%              % ruled table alignment
  366.    \@RuledTable#1\endruledtable}%               % same as \ruledtable
  367.  
  368. %---------------------------------------*
  369. % Turn off @ as letter before we exit
  370.  
  371. \catcode`@=12                                   % @ is not a letter
  372.  
  373. %>>> EOF TXSruled.tex <<<
  374.